package com.yf.yfwebview

import android.content.ComponentName
import android.content.Context
import android.content.Intent
import android.content.ServiceConnection
import android.os.IBinder
import android.util.Log
import com.android.baselibrary.app.MainApplication
import com.yf.yfwebview.mainprocess.WebViewCommandService

object WebViewCommandDispatcher : ServiceConnection {
    private var handleCommandInterface: IWebViewHandleCommondInterface? = null

    fun handleCommand(name: String, param: String) {
        Log.i("WebViewCommandDispatcher", "onServiceConnected: handleCommand！")
        handleCommandInterface?.handleCommond(name, param)
    }
   fun initAidl(){
       val intent = Intent(MainApplication.mainApplication,WebViewCommandService::class.java)

      // intent.setComponent(ComponentName("com.yf.yfwebview","com.yf.yfwebview.mainprocess.WebViewCommandService"))
       Log.i("WebViewCommandDispatcher", "initAidl: handleCommand！")
       MainApplication.mainApplication.bindService(intent,this,Context.BIND_AUTO_CREATE)
   }


    override fun onServiceConnected(name: ComponentName?, service: IBinder?) {
        Log.i("WebViewCommandDispatcher", "onServiceConnected: 绑定成功！")
        handleCommandInterface = IWebViewHandleCommondInterface.Stub.asInterface(service)
    }

    override fun onServiceDisconnected(name: ComponentName?) {
        Log.i("WebViewCommandDispatcher", "onServiceConnected: 解绑成功！")
        handleCommandInterface = null
    }

}